<template>
  <div class="productList">
    <div class="productCateTitle">
      <div class="productCateTitleL">
        <i class="icon iconfont icon-liebiao"></i>
        类型数据列表
      </div>
      <div class="productCateTitleR">
        <el-button @click="openadd" type="primary">添加</el-button>
      </div>
    </div>
    <div class="productCateBody">
      <el-table :data="ProductAttrList" border style="width: 100%">
        <el-table-column prop="id" label="编号"> </el-table-column>
        <el-table-column
          prop="name"
          label="类型名称"
          width="600%"
        ></el-table-column>
        <el-table-column
          prop="attribute_count"
          label="属性数量"
        ></el-table-column>
        <el-table-column prop="param_count" label="参数数量"> </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <div class="tableBtn">
              <el-button @click="openupdata(scope.row.name,scope.row.id)" class="updata" type="primary">编辑</el-button>
              <el-button class="delete" type="danger">删除</el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="block">
      <el-pagination
        :current-page="listQuery.pageNum"
        @current-change="handleCurrentChange"
        layout="total,sizes,prev, pager, next"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="listQuery.pageSize"
        @size-change="handleSizeChange"
        :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import { fetchProductAttrList } from "@/api/product";
import { insertProductAttrList } from "@/api/product";
import { updataProductAttrList } from "@/api/product";

export default {
  name: "productAttr",
  data() {
    return {
      ProductAttrList: [],
      listQuery: {
        pageNum: 1,
        pageSize: 5,
      },
      total: 0,
      addAttr: {
        name: "",
      },
			updataAttr:{
				id:0,
				name:""
			}
    };
  },
  mounted() {
    this.getProductAttrList();
  },
  methods: {
    async getProductAttrList() {
      let res = await fetchProductAttrList(this.listQuery);
      this.ProductAttrList = res.data;
      this.total = res.total;
      console.log(this.ProductAttrList);
    },
    handleCurrentChange: function (val) {
      this.listQuery.pageNum = val;
      this.getProductAttrList();
    },
    handleSizeChange: function (val) {
      this.listQuery.pageSize = val;
      this.getProductAttrList();
    },
    openadd() {
      this.$prompt("请输入类型名", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
      }).then(({ value }) => {
          this.addAttr.name = value;
          insertProductAttrList(this.addAttr).then((res) => {
						console.log(res);
            if (res.status === 200) {
              this.$message({
                type: "success",
                message: "添加成功",
              });
							this.getProductAttrList()
            }
          });
        }).catch(() => {
          this.$message({
            type: "info",
            message: "取消添加",
          });
        });
    },
		openupdata(NAME,id){
			this.$prompt("请输入类型名", "修改", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
				inputValue: NAME
      }).then(({ value }) => {
          this.updataAttr.name = value;
					this.updataAttr.id = id;
          updataProductAttrList(this.updataAttr).then((res) => {
						console.log(res);
            if (res.status === 200) {
              this.$message({
                type: "success",
                message: "修改成功",
              });
							this.getProductAttrList()
            }
          });
        }).catch(() => {
          this.$message({
            type: "info",
            message: "取消修改",
          });
        });
		}
  },
};
</script>

<style>
.productCateTitle {
  display: flex;
  justify-content: space-between;
  padding: 20px;
  border: 1px solid #ebeef5;
}

.productCateTitleR > button {
  width: 64px;
  height: 24px;
  padding: 0;
}

.productCateTitleR button:first-child {
  width: 90px;
  height: 24px;
  padding: 0;
}

.tableBtn {
  display: flex;
}

.updata,
.delete {
  width: 56px;
  height: 28px;
  padding: 0;
  color: #fff;
}

.down {
  width: 80px;
  height: 28px;
  padding: 0;
}

.el-table td,
.el-table th {
  text-align: center;
}
</style>